/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import predicates.CandidateCapableIsValidPredicate;
import util.filter.CollectionsFilter;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "company_folder")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CompanyFolder.findAll", query = "SELECT c FROM CompanyFolder c"),
    @NamedQuery(name = "CompanyFolder.findByIdCompanyFolder", query = "SELECT c FROM CompanyFolder c WHERE c.idCompanyFolder = :idCompanyFolder"),
    @NamedQuery(name = "CompanyFolder.findByDescription", query = "SELECT c FROM CompanyFolder c WHERE c.description = :description"),
    @NamedQuery(name = "CompanyFolder.findByCompanyFolderTypeAndCompany", query = "SELECT c FROM CompanyFolder c WHERE c.companyFolderType.idCompanyFolderType = :idCompanyFolderType AND c.idCompany.idCompany = :idCompany"),
    @NamedQuery(name = "CompanyFolder.findByCreationDate", query = "SELECT c FROM CompanyFolder c WHERE c.creationDate = :creationDate")})
public class CompanyFolder implements Serializable {
    @OneToMany(mappedBy = "folder")
    private List<CompanyAdquiredProfile> companyAdquiredProfileList;
    @JoinColumn(name = "COMPANY_FOLDER_TYPE", referencedColumnName = "ID_COMPANY_FOLDER_TYPE")
    @ManyToOne(optional = false)
    private CompanyFolderType companyFolderType;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "companyFolder")
    private List<CompanyAdquiredProfileFolder> companyAdquiredProfileFolderList;
    @OneToMany(mappedBy = "folder", fetch = FetchType.LAZY)
    private List<Search> searchList;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_COMPANY_FOLDER")
    private Integer idCompanyFolder;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "DESCRIPTION")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CREATION_DATE")
    @Temporal(TemporalType.DATE)
    private Date creationDate;
    @JoinColumn(name = "ID_COMPANY", referencedColumnName = "ID_COMPANY")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Company idCompany;
    

    public CompanyFolder() {
    }

    public CompanyFolder(Integer idCompanyFolder) {
        this.idCompanyFolder = idCompanyFolder;
    }

    public CompanyFolder(Integer idCompanyFolder, String description, Date creationDate) {
        this.idCompanyFolder = idCompanyFolder;
        this.description = description;
        this.creationDate = creationDate;
    }

    public Integer getIdCompanyFolder() {
        return idCompanyFolder;
    }

    public void setIdCompanyFolder(Integer idCompanyFolder) {
        this.idCompanyFolder = idCompanyFolder;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Company getIdCompany() {
        return idCompany;
    }

    public void setIdCompany(Company idCompany) {
        this.idCompany = idCompany;
    }

    public List<CompanyAdquiredProfile> getCompanyAdquiredProfileList() {
        ArrayList<CompanyAdquiredProfile> profiles=new ArrayList<CompanyAdquiredProfile>();
        if(companyAdquiredProfileFolderList!=null){
            for (CompanyAdquiredProfileFolder companyAdquiredProfileFolder : companyAdquiredProfileFolderList) {
                if(!profiles.contains(companyAdquiredProfileFolder.getCompanyAdquiredProfile())){
                   profiles.add(companyAdquiredProfileFolder.getCompanyAdquiredProfile());
                }
            }
        }
        return profiles;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCompanyFolder != null ? idCompanyFolder.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CompanyFolder)) {
            return false;
        }
        CompanyFolder other = (CompanyFolder) object;
        if ((this.idCompanyFolder == null && other.idCompanyFolder != null) || (this.idCompanyFolder != null && !this.idCompanyFolder.equals(other.idCompanyFolder))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.CompanyFolder[ idCompanyFolder=" + idCompanyFolder + " ]";
    }

    @XmlTransient
    public List<Search> getSearchList() {
        return searchList;
    }

    public void setSearchList(List<Search> searchList) {
        this.searchList = searchList;
    }

    @XmlTransient
    public List<CompanyAdquiredProfileFolder> getCompanyAdquiredProfileFolderList() {
        return companyAdquiredProfileFolderList;
    }

    public void setCompanyAdquiredProfileFolderList(List<CompanyAdquiredProfileFolder> companyAdquiredProfileFolderList) {
        this.companyAdquiredProfileFolderList = companyAdquiredProfileFolderList;
    }

    public CompanyFolderType getCompanyFolderType() {
        return companyFolderType;
    }

    public void setCompanyFolderType(CompanyFolderType companyFolderType) {
        this.companyFolderType = companyFolderType;
    }

    public void setCompanyAdquiredProfileList(List<CompanyAdquiredProfile> companyAdquiredProfileList) {
        this.companyAdquiredProfileList = companyAdquiredProfileList;
    }
    
    public List<CompanyAdquiredProfileFolder> getValidCompanyAdquiredProfileFolderList(){
        if(this.getCompanyFolderType().getIdCompanyFolderType()==2){
            return CollectionsFilter.filterOnNewCollection(getCompanyAdquiredProfileFolderList(), new CandidateCapableIsValidPredicate<CompanyAdquiredProfileFolder>());
        }
        else{
            return getCompanyAdquiredProfileFolderList();
        }
    }
    
}
